from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker




Base = declarative_base()
engine = create_engine('mysql+pymysql://root:123456@localhost/test')
Base.metadata.create_all(engine)


Session = sessionmaker(bind=engine)
session = Session()


# 创建一个新用户
new_user = User email='john@example.com')
session.add(new_user)
session.commit()

# 查询所有用户
users = session.query(User).all()
for user in users:
    print(user.name, user.email)

# 查询特定条件的用户
user = session.query(User).filter_by(name='John').first()
print(user.name, user.email)

# 更新用户
user.name = 'John Doe'
session.commit()

# 删除用户
session.delete(user)
session.commit()